﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Rated.Common.Classes;
using Rated.DAL.Db;
using Rated.DAL.Repository;
using Rated.Website.Helpers;

namespace Rated.Website.Controllers
{
    public class ReviewController : Controller
    {
        [HttpPost]
        public ActionResult Add()
        {
            var model = ReviewRepository.Add(Request.Form);
            var view = ControllerHelper.RenderPartialViewToString(this, "Review", model);
            var reviewCount = model.RatedItem.Reviews.Count;
            return Json(new {view = view, reviewCount = reviewCount});
        }

        [HttpPost]
        public ActionResult Edit()
        {
            var model = ReviewRepository.Edit(Request.Form);
            var view = ControllerHelper.RenderPartialViewToString(this, "Review", model);
            var reviewCount = model.RatedItem.Reviews.Count;
            return Json(new { view = view, reviewCount = reviewCount });
        }

        [HttpPost]
        public ActionResult Delete()
        {
            var itemID = int.Parse(Request["itemID"]);
            // we want to delete this item
            return PartialView("RatingsList", new Rated.Website.Models.ViewPageViewModel(itemID));
        }

        [HttpPost]
        public ActionResult Respond()
        {
            var itemID = int.Parse(Request["itemID"]);
            Rating r = new Rating();
            r.Rator = new Member();
            r.Rator.Name = "Peter Phillips";
            r.Rator.DisplayName = "Peter Phillips";
            r.Rator.Location = "Porthcawl, South Wales";
            r.StarRating = 3f;
            r.Title = "Updated";
            r.Description = "very Good";
            r.Timestamp = DateTime.Now;
            r.ID = 10;
            // Really need to return a Json object here containing
            // 1. The view of the newly added rating
            // 2. The Updated overall rating for the item
            // 3. the count of the actual reviews
            r.Response = "Here's a response";
            return PartialView("ItemRating", r);
        }

    }
}
